import { login, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // 用户登录
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo)
        .then((response) => {
          const { data } = response
          // 这里需要调整，读取token的深浅不一致
          // 触发mutations
          commit('SET_TOKEN', data.token)
          // 存储到本地
          setToken(data.token)
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 获取用户资料
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          return reject('验证失败，请重新登录！')
        }
        // 读取的字段可能不一致
        console.log(data)
        const { userinfo } = data
        // 设置给state
        commit('SET_NAME', userinfo.username)
        console.log(userinfo.username)
        commit('SET_AVATAR', userinfo.avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 退出操作
  logout({ commit, state }) {
    removeToken()
    // 重置路由
    resetRouter()
    // 重置state数据
    commit('RESET_STATE')
  },
  // 移除token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      // 先移除token
      removeToken()
      // 重置state数据
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
